package org.xue.snowmall.bargain.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import entity.UserRecordEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.xue.snowmall.bargain.entity.BargainEntity;
import org.xue.snowmall.bargain.entity.BargainRecordEntity;
import org.xue.snowmall.bargain.service.BargainRecordService;
import org.xue.snowmall.bargain.service.BargainService;
import org.xue.snowmall.bargain.utile.BargainUtils;
import org.xue.snowmall.common.utils.PageUtils;
import org.xue.snowmall.common.utils.Query;

import org.xue.snowmall.bargain.dao.HelpRecordDao;
import org.xue.snowmall.bargain.entity.HelpRecordEntity;
import org.xue.snowmall.bargain.service.HelpRecordService;
import org.xue.snowmall.common.utils.R;

import javax.servlet.http.HttpSession;


@Service("helpRecordService")
public class HelpRecordServiceImpl extends ServiceImpl<HelpRecordDao, HelpRecordEntity> implements HelpRecordService {

    @Autowired
    private BargainRecordService bargainRecordService;

    @Autowired
    private HttpSession session;

    @Autowired
    private BargainService bargainService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<HelpRecordEntity> page = this.page(
                new Query<HelpRecordEntity>().getPage(params),
                new QueryWrapper<HelpRecordEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public R saveHelpRecord(Long gsmBargainRecordId) {

        //获取当前用户信息
        UserRecordEntity user = (UserRecordEntity) session.getAttribute("user");
        //判断当前用户是不是购买商品的用户 不能帮自己砍价
        BargainRecordEntity bargainRecord = bargainRecordService.getById(gsmBargainRecordId);

        //去活动表中查询当前的策略
        BargainEntity bargainEntity = bargainService.getById(bargainRecord.getGsmBargainId());

        if (bargainRecord.getUsmId().equals(user.getUsmId())) {
            //自己不能为自己砍价
            return R.error("不能给自己砍价");
        }
        //判断当前用户是不是二次砍价 一个用户只能砍一次
        HelpRecordEntity usm_id = baseMapper.selectOne(new QueryWrapper<HelpRecordEntity>().eq("usm_id", user.getUsmId()));
        if (usm_id != null) {
            //用户二次砍价
            return R.error("只能帮砍该用户一次");
        }
        //判断当前价格是否等于底价
        //获取当前价格
        BigDecimal gsmBargainPriceNow = bargainRecord.getGsmBargainPriceNow();
        //获取底价
        BigDecimal psmBargainPrice = bargainEntity.getPsmBargainPrice();

        //修改砍价表
        //次数加一
        bargainRecord.setGsmBargainCountNow(bargainRecord.getGsmBargainCountNow() + 1);

        //判断砍价类型
        String gsmRuleType = bargainEntity.getGsmRuleType();

        Integer newPrice = null;
        Integer i = null;
        //刀数限制
        if (gsmRuleType.equals("0")) {
            //获取刀数限制
            Integer gsmBargainMaxCount = bargainEntity.getGsmBargainMaxCount();
            //砍价逻辑,修改当前的价格
            newPrice = BargainUtils.reduceCount(gsmBargainPriceNow.intValue(), psmBargainPrice.intValue(), gsmBargainMaxCount);
            i = gsmBargainPriceNow.intValue() - newPrice;
        }
        //每次金额限制
        if (gsmRuleType.equals("1")) {
            //获取金额限制
            BigDecimal gsmBargainMaxPrice = bargainEntity.getGsmBargainMaxPrice();
            //砍价逻辑,修改当前的价格
            newPrice = BargainUtils.reducePrice(gsmBargainPriceNow.intValue(), psmBargainPrice.intValue(), gsmBargainMaxPrice.intValue());
            i = gsmBargainPriceNow.intValue() - newPrice;
        }
        bargainRecord.setGsmBargainPriceNow(new BigDecimal(i+""));
        bargainRecordService.updateById(bargainRecord);
        //添加帮砍表
        HelpRecordEntity helpRecordEntity = new HelpRecordEntity();
        helpRecordEntity.setUsmId(user.getUsmId());
        helpRecordEntity.setGsmBargainRecordId(gsmBargainRecordId);
        baseMapper.insert(helpRecordEntity);

        //此时所有数据都已结束 ,判断是否还能继续砍价
        if (gsmBargainPriceNow.equals(psmBargainPrice)) {
            //修改记录表状态
            bargainRecord.setGsmBargainRecordStatus("1");
            bargainRecordService.updateById(bargainRecord);
        }
        return R.ok();
    }

}